//
//  WithdrawDetailViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/11/15.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "WithDrawDetailModel.h"

#import "WithDrawProgressCell.h"
#import "WithDrawProgressHeadCell.h"

#import "WithdrawDetailViewController.h"

@interface WithdrawDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

//@property (nonatomic, assign) NSInteger pageNum;
//@property (nonatomic, assign) NSInteger pageSize;

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) WithDrawDetailModel *model;

@end

@implementation WithdrawDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_withDrawDetail];
}

- (void)request_withDrawDetail {
    
    self.model.relationType = self.relationType;
    
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.relationId forKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_WITHDRAW_DETAIL];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    self.model.amount = [[resultDic valueForKey:@"amount"] doubleValue];
                    self.model.status = (PPWithdrawResultType)[[resultDic valueForKey:@"status"] integerValue];
                    
                    WithDrawProgressModel *model1 = [[WithDrawProgressModel alloc] init];
                    model1.itemTitle = [NSString stringWithFormat:@"%@元提现申请已提交",[Utilities changeDoubleToString:self.model.amount]];
                    
                    model1.itemMemo = @"";
                    model1.isFinish = YES;
                    model1.isFirst = YES;
                    NSTimeInterval applyTime = [[resultDic valueForKey:@"applyTime"] doubleValue];
                    model1.itemTime = [Utilities getTimeFromTimestamp:applyTime];
                    [self.model.progressArray addObject:model1];

                    WithDrawProgressModel *model2 = [[WithDrawProgressModel alloc] init];
                    model2.itemTitle = @"处理中...";
                    model2.itemMemo = @"非工作日到账会有延迟，预计1-3个工作日";
                    model2.itemTime = @"";
                    model2.isFinish = YES;
                    [self.model.progressArray addObject:model2];
                    
                    WithDrawProgressModel *model3 = [[WithDrawProgressModel alloc] init];
                    model3.itemMemo = [resultDic valueForKey:@"reason"];
                    
                    NSTimeInterval auditTime = [[resultDic valueForKey:@"auditTime"] doubleValue];
                    if (auditTime) {
                        model3.itemTime = [Utilities getTimeFromTimestamp:auditTime];
                    } else {
                        model3.itemTime = @"";
                    }
                    
                    switch (self.model.status) {
                        case PPWithdrawResultType_Wait:{
                            
                            model3.itemTitle = @"等待到账";
                            model3.isFinish = NO;
                            model3.isLast = YES;
                           } break;
                            
                        case PPWithdrawResultType_Success:{
                            
                            model3.itemTitle = @"已到账";
                            model3.isFinish = YES;
                            model3.isLast = YES;
                            
                        } break;
                        case PPWithdrawResultType_Fail:{
                            
                            model3.itemTitle = @"审核不通过";
                            model3.isFinish = YES;
                            model3.isLast = YES;
                            
                        } break;
                        default:
                            break;
                    }
                    
                    [self.model.progressArray addObject:model3];

                }
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        } else {
            
        }
        
        [self.tableview reloadData];
        
    } FailedBlock:^(NSError * _Nonnull error) {
                
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{

        }];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        WithDrawProgressHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithDrawProgressHeadCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        [cell fillCellWithModel:self.model];
        return cell;
    } else {
        WithDrawProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithDrawProgressCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        [cell fillCellWithModel:self.model.progressArray[indexPath.row - 1]];
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.progressArray.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        return 200;
    } else {
        return 100;
    }
}

- (void)initView {
    
    self.title = @"提现详情";
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"WithDrawProgressCell" bundle:nil] forCellReuseIdentifier:@"WithDrawProgressCellID"];

    [self.tableview registerNib:[UINib nibWithNibName:@"WithDrawProgressHeadCell" bundle:nil] forCellReuseIdentifier:@"WithDrawProgressHeadCellID"];
}

- (WithDrawDetailModel *)model {
    
    if (!_model) {
        _model = [[WithDrawDetailModel alloc] init];
        _model.progressArray = @[].mutableCopy;
    }
    return _model;
}

@end
